package main

import (
	"fmt"
	"time"
)

func main() {
	ch := make(chan int)
	mainShutDownFlagCh := make(chan struct{})
	// 生产者
	go func(ch chan int) {
		for i := 0; i < 5; i++ {
			ch <- i
			time.Sleep(time.Second)
		}
		close(ch)
	}(ch)

	go func() {
		// 接受到close信号会自动退出循环
		for v := range ch {
			fmt.Println(v)
		}
		fmt.Println("已关闭，准备发送信息给main，关闭main线程")
		mainShutDownFlagCh <- struct{}{}
	}()

	<-mainShutDownFlagCh
}
